//
//  InformationTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/10/25.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "InformationTableViewCell.h"
#import "Articles.h"
#import "LZPictureBrowser.h"
#import <Masonry.h>

@implementation InformationTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self CreatUI];
    }
    return self;
}

- (void)CreatUI
{
    
    _title = [[UILabel alloc] init];
    [self.contentView addSubview:_title];
    _title.textColor = kBlackColor;
    _title.font = kUHSystemFontWithSize(16);
    _title.numberOfLines = 2;
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.left.mas_offset(10);
        make.width.mas_offset(SCREEN_WIDTH-20);
    }];
    
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
    //    _bgView.backgroundColor = kRedColor;
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_title.mas_bottom).mas_offset(10);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_offset(0);
    }];

    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {
        UIImageView *img = [[UIImageView alloc] init];
        [_bgView addSubview:img];
        img.hidden = YES;
        img.contentMode = UIViewContentModeScaleAspectFit;
        [arr addObject:img];
    }
    self.imgArr = [NSArray arrayWithArray:arr];
    
    
    CGSize size2 = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(12)];
    
    _isfine = [[UILabel alloc] init];
    [self.contentView addSubview:_isfine];
    _isfine.textColor = kRedColor;
    _isfine.font = kUHSystemFontWithSize(11);
    _isfine.layer.borderColor = kRedColor.CGColor;
    _isfine.layer.borderWidth = 1;
    _isfine.layer.cornerRadius = 3;
    _isfine.layer.masksToBounds = YES;
    _isfine.text = @"置顶";
    _isfine.textAlignment = NSTextAlignmentCenter;
    CGSize fineS = [LZCTools textSizeWithText:@"字数" font:_isfine.font];
    [_isfine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView.mas_bottom).offset(0);
        make.left.mas_offset(0);
        make.height.mas_offset(size2.height);
        make.width.mas_offset(0);
    }];
    
    _num = [[UILabel alloc] init];
    [self.contentView addSubview:_num];
    _num.textColor = kLightGrayColor;
    _num.font = kUHSystemFontWithSize(12);
    [_num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView.mas_bottom).offset(0);
        make.left.mas_equalTo(_isfine.mas_right).offset(10);
        make.height.mas_offset(size2.height);
        make.bottom.mas_offset(-10);
    }];
    
    _time = [[UILabel alloc] init];
    [self.contentView addSubview:_time];
    _time.textColor = kLightGrayColor;
    _time.font = kUHSystemFontWithSize(12);
    [_time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView.mas_bottom).offset(0);
        make.left.mas_equalTo(_num.mas_right).offset(10);
        make.height.mas_offset(size2.height);
        make.bottom.mas_offset(-10);
    }];

}

- (void)setModel:(Articles *)model {
    if (_model != model) {
        _model = model;
        
        self.title.text = model.title;
        NSMutableAttributedString *lsStr = [[NSMutableAttributedString alloc] initWithString:self.title.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:4];//调整行间距
        [lsStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.title.text length])];
        self.title.attributedText = lsStr;
        
        self.num.text = [NSString stringWithFormat:@"%ld评论", model.comments_count];
        self.time.text = model.human_time;
        
        // 清空之前的数据
//        [_bgView removeAllSubViews];
        for (UIImageView *imgView in self.imgArr) {
            //            imgView.image = [UIImage imageNamed:@""];
            imgView.hidden = YES;
        }
        [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(0);
        }];
        
        
        
        if (model.images.count > 0) {
            
            // 取屏
            self.mArr = [[NSMutableArray alloc] init];
            [self.mArr addObjectsFromArray:model.images];
            NSArray *arr = model.images;
            
            CGFloat imgW = (SCREEN_WIDTH-40)/3;
            for (int i = 0; i < arr.count; i++) {
                
                
                UIImageView *imageView = self.imgArr[i];
                imageView.hidden = NO;
                [imageView addTarget:self action:@selector(imageClick:)];
                [imageView sd_setImageWithURL:[NSURL URLWithString:arr[i]] placeholderImage:IMAGE(@"placeHolder")];
                imageView.tag = 1+i;
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                
                imageView.frame = CGRectMake((imgW+10)*i,0, imgW, imgW);
            }
            [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_offset(imgW+10);
            }];
            
        }
        
        CGSize fineS = [LZCTools textSizeWithText:@"字数" font:_isfine.font];
        if (model.istop == 1) {
            [_isfine mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(10);
                make.width.mas_offset(fineS.width+fineS.width/4);
            }];
        }
        else {
            [_isfine mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(0);
                make.width.mas_offset(0);
            }];
        }
        
        [self updateConstraints];
        
    }
}

- (void)imageClick:(UIImageView *)img {
    
    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer
                                             *)img;
    
    LZPictureBrowser *mvc = [[LZPictureBrowser alloc] init];
    [mvc showWithPictureURLs:self.mArr atIndex:[tapRecognizer.view tag]-1];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
